/**
 * Copyright 2016 vip.com.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 * </p>
 **/

package spring.cloud.tasks.api.database.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import spring.cloud.tasks.api.database.entity.NameSpaceZooKeeperClusterMappingDto;
import spring.cloud.tasks.api.database.repository.NameSpaceZooKeeperClusterMappingDtoRepository;
import spring.cloud.tasks.api.database.service.NameSpaceZooKeeperClusterMappingDtoService;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;


@Service
public class NameSpaceZooKeeperClusterMappingDtoServiceImpl implements NameSpaceZooKeeperClusterMappingDtoService {

    @Autowired
    private NameSpaceZooKeeperClusterMappingDtoRepository nameSpaceZooKeeperClusterMappingDtoRepository;

    private List<NameSpaceZooKeeperClusterMappingDto> getActiveNamespaceZooKeeperClusterMappingDtoList(List<NameSpaceZooKeeperClusterMappingDto> dtoList) {
        List<NameSpaceZooKeeperClusterMappingDto> list = new ArrayList<>();
        if (dtoList != null) {
            Iterator<NameSpaceZooKeeperClusterMappingDto> iterator = dtoList.iterator();
            while (iterator.hasNext()) {
                NameSpaceZooKeeperClusterMappingDto next = iterator.next();
                if (next.getIsDeleted() == 0) {
                    list.add(next);
                }
            }
        }
        return list;
    }

    @Transactional(readOnly = true)
    @Override
    public List<NameSpaceZooKeeperClusterMappingDto> getNameSpaceZooKeeperClusterMappingDtoList() {
        return getActiveNamespaceZooKeeperClusterMappingDtoList(nameSpaceZooKeeperClusterMappingDtoRepository.selectAll());
    }

    @Transactional(readOnly = true)
    @Override
    public List<NameSpaceZooKeeperClusterMappingDto> getNameSpaceZooKeeperClusterMappingDtoList(String zooKeeperClusterKey) {
        return getActiveNamespaceZooKeeperClusterMappingDtoList(nameSpaceZooKeeperClusterMappingDtoRepository.selectByZooKeeperClusterKey(zooKeeperClusterKey));
    }

    @Transactional(readOnly = true)
    @Override
    public List<String> getNameSpaceList(String zooKeeperClusterKey) {
        List<String> result = new ArrayList<String>();
        List<NameSpaceZooKeeperClusterMappingDto> dtoList = getActiveNamespaceZooKeeperClusterMappingDtoList(nameSpaceZooKeeperClusterMappingDtoRepository.selectByZooKeeperClusterKey(zooKeeperClusterKey));
        for (NameSpaceZooKeeperClusterMappingDto dto : dtoList) {
            result.add(dto.getNameSpace());
        }
        return result;
    }

    @Transactional(readOnly = true)
    @Override
    public String getZooKeeperClusterKey(String nameSpace) {
        NameSpaceZooKeeperClusterMappingDto dto = nameSpaceZooKeeperClusterMappingDtoRepository.selectByNameSpace(nameSpace);
        if (dto == null) {
            return null;
        }
        if (dto.getIsDeleted() == 1) {
            return null;
        }
        return dto.getZooKeeperClusterKey();
    }

    @Transactional

    public Integer insert(String nameSpace, String zooKeeperClusterKey, String createdBy) {
        NameSpaceZooKeeperClusterMappingDto dto = nameSpaceZooKeeperClusterMappingDtoRepository.selectByNameSpace(nameSpace);
        boolean insert;
        if (dto == null) {
            dto = new NameSpaceZooKeeperClusterMappingDto();
            insert = true;
        } else {
            insert = false;
        }
        Date now = new Date();
        dto.setCreatedBy(createdBy);
        dto.setCreateDateTime(now);
        dto.setLastUpdatedBy(createdBy);
        dto.setLastUpdateDateTime(now);
        //
        dto.setNameSpace(nameSpace);

        dto.setZooKeeperClusterKey(zooKeeperClusterKey);
        dto.setIsDeleted(0);
        if (insert) {
            return nameSpaceZooKeeperClusterMappingDtoRepository.insert(dto);
        } else {
            return nameSpaceZooKeeperClusterMappingDtoRepository.updateAllById(dto);
        }
    }

    @Transactional
    @Override
    public Integer remove(String nameSpace, String lastUpdatedBy) {
        NameSpaceZooKeeperClusterMappingDto dto = nameSpaceZooKeeperClusterMappingDtoRepository.selectByNameSpace(nameSpace);
        dto.setIsDeleted(1);
        dto.setLastUpdatedBy(lastUpdatedBy);
        return nameSpaceZooKeeperClusterMappingDtoRepository.updateById(dto);
    }

    @Transactional
    @Override
    public Integer update(String nameSpace, String name, String zooKeeperClusterKey, String lastUpdatedBy) {
        NameSpaceZooKeeperClusterMappingDto dto = nameSpaceZooKeeperClusterMappingDtoRepository.selectByNameSpace(nameSpace);

        if (zooKeeperClusterKey != null) {
            dto.setZooKeeperClusterKey(zooKeeperClusterKey);
        }
        dto.setLastUpdatedBy(lastUpdatedBy);
        return nameSpaceZooKeeperClusterMappingDtoRepository.updateById(dto);
    }
}
